package com.lenovo.codestatreport.jira.service;

import com.lenovo.codestatreport.jira.mapper.AssigneeAndReporterMapper;
import com.lenovo.codestatreport.jira.model.AssigneeAndReporter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author Howie Lu
 * @version Created at 2019/05/20
 * @version Updated at 2019/05/21
 */
@Service("AssigneeAndReporterService")
public class AssigneeAndReporterServiceImpl implements AssigneeAndReporterService {
    @Autowired
    AssigneeAndReporterMapper assigneeAndReporterMapper;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Override
    synchronized public List<AssigneeAndReporter> findAllByTime(String tablename, String start, String end) {
        RedisSerializer redisSerializer = new StringRedisSerializer();
        String key = tablename + "/From" + start + "to" + end;
        redisTemplate.setKeySerializer(redisSerializer);
        List<AssigneeAndReporter> list = (List<AssigneeAndReporter>) redisTemplate.opsForValue().get(key);
        if (list == null) {
            list = assigneeAndReporterMapper.selectAllByTime(tablename, start, end);
            redisTemplate.opsForValue().set(key, list, 120, TimeUnit.SECONDS);
            //  System.out.println(key + " 缓存中没找到，访问数据库！");
        }// else System.out.println(key + " 缓存中找到了，无需访问数据库！");
        return list;
    }

    @Override
    public List<AssigneeAndReporter> findAll(String tablename) {
        RedisSerializer redisSerializer = new StringRedisSerializer();
        String key = tablename + "/FindAll" ;
        redisTemplate.setKeySerializer(redisSerializer);
        List<AssigneeAndReporter> list = (List<AssigneeAndReporter>) redisTemplate.opsForValue().get(key);
        if (list == null) {
            list = assigneeAndReporterMapper.selectAll(tablename);
            redisTemplate.opsForValue().set(key, list, 120, TimeUnit.SECONDS);
            //  System.out.println(key + " 缓存中没找到，访问数据库！");
        }// else System.out.println(key + " 缓存中找到了，无需访问数据库！");
        return list;
    }

    @Override
    public List<AssigneeAndReporter> findByProjectKey(String tablename, String projectkey) {
        RedisSerializer redisSerializer = new StringRedisSerializer();
        String key = tablename + "/FindByProjectKey" + projectkey;
        redisTemplate.setKeySerializer(redisSerializer);
        List<AssigneeAndReporter> list = (List<AssigneeAndReporter>) redisTemplate.opsForValue().get(key);
        if (list == null) {
            list = assigneeAndReporterMapper.selectByProjectKey(tablename, projectkey);
            redisTemplate.opsForValue().set(key, list, 120, TimeUnit.SECONDS);
            //  System.out.println(key + " 缓存中没找到，访问数据库！");
        }// else System.out.println(key + " 缓存中找到了，无需访问数据库！");
        return list;
    }

    @Override
    public List<String> findAllProjects(String tablename) {
        RedisSerializer redisSerializer = new StringRedisSerializer();
        String key = tablename + "/FindAllProjects";
        redisTemplate.setKeySerializer(redisSerializer);
        List<String> list = (List<String>) redisTemplate.opsForValue().get(key);
        if (list == null) {
            list = assigneeAndReporterMapper.selectAllProjects(tablename);
            redisTemplate.opsForValue().set(key, list, 120, TimeUnit.SECONDS);
            //  System.out.println(key + " 缓存中没找到，访问数据库！");
        }// else System.out.println(key + " 缓存中找到了，无需访问数据库！");
        return list;
    }

    @Override
    public List<AssigneeAndReporter> findByStaffByTime(String tablename, String staffId, String start, String end) {
        RedisSerializer redisSerializer = new StringRedisSerializer();
        String key = tablename + "/From" + start + "to" + end + "staffId" + staffId;
        redisTemplate.setKeySerializer(redisSerializer);
        List<AssigneeAndReporter> list = (List<AssigneeAndReporter>) redisTemplate.opsForValue().get(key);
        if (list == null) {
            list = assigneeAndReporterMapper.selectByStaffByTime(tablename, staffId, start, end);
            redisTemplate.opsForValue().set(key, list, 120, TimeUnit.SECONDS);
            //  System.out.println(key + " 缓存中没找到，访问数据库！");
        }// else System.out.println(key + " 缓存中找到了，无需访问数据库！");
        return list;
    }

    @Override
    public List<AssigneeAndReporter> findByProjectKeyByTime(String tablename, String projectkey, String start, String end) {
        RedisSerializer redisSerializer = new StringRedisSerializer();
        String key = "ProjectKey: " + projectkey + "Table: " + tablename + "/From" + start + "to" + end;
        redisTemplate.setKeySerializer(redisSerializer);
        List<AssigneeAndReporter> list = (List<AssigneeAndReporter>) redisTemplate.opsForValue().get(key);
        if (list == null) {
            list = assigneeAndReporterMapper.selectByProjectKeyByTime(tablename, projectkey, start, end);
            redisTemplate.opsForValue().set(key, list, 120, TimeUnit.SECONDS);
            //  System.out.println(key + " 缓存中没找到，访问数据库！");
        }// else System.out.println(key + " 缓存中找到了，无需访问数据库！");
        return list;
    }
}
